//
//  WQCWelcomController.swift
//  3-微博
//
//  Created by DDDD on 16/3/1.
//  Copyright © 2016年 gongsimingzi. All rights reserved.
//

import UIKit
import SDWebImage


class WQCWelcomController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view = backgroudImageView
        
        setupUI()
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear( animated)
        startAnimation()
    }
    private func setupUI(){
    
        view.addSubview(iconImageView)
        view.addSubview(nameLebal)
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-150)
            make.centerX.equalTo(view.snp_centerX)
//            make.size.equalTo(CGSizeMake(170/2, 170/2))
        }
        nameLebal.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom)
            make.centerX.equalTo(view.snp_centerX)
        }
        let url = NSURL(string: WQCUserViewModel.userViewModel.userModel?.profile_image_url ?? "")
        if let Ourl = url{
            iconImageView.sd_setImageWithURL(Ourl, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        if let Olebal = WQCUserViewModel.userViewModel.userModel?.screen_name {
            nameLebal.text = Olebal
        }
    }
    
    private func startAnimation(){
    
        self.iconImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-UIScreen.mainScreen().bounds.height + 100)
            make.centerX.equalTo(self.view.snp_centerX)
            make.size.equalTo(CGSizeMake(170/2, 170/2))
        }
        nameLebal.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom)
            make.centerX.equalTo(view.snp_centerX)
        }

        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                NSNotificationCenter.defaultCenter().postNotificationName(kNotificaionChangeRootVC, object: nil)
        }
    }
    
    private lazy var iconImageView : UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        img.layer.cornerRadius = 170 / 4
        
        img.layer.masksToBounds = true
        
        img.layer.backgroundColor = UIColor.grayColor().CGColor
        
        img.layer.borderWidth = 2
        
        return img
    }()
    
    private var backgroudImageView : UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "ad_background")
        
        return img
    }()
    
    private var nameLebal : UILabel = {
        let namelebal = UILabel()
        
        namelebal.text = "欢迎回来"
        
        namelebal.textColor = UIColor.darkGrayColor()
        
        namelebal.textAlignment = NSTextAlignment.Center
        
        namelebal.font = UIFont.systemFontOfSize( 14 )
        
        return namelebal
    }()
}
